﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAO;
using DataAccess;
using System.Data;
namespace BUL
{
   public class NextOfKindBUL
    {
        ConnectDatabase cd;
        DataTable dt;
        public NextOfKindBUL() {
            cd = new ConnectDatabase();
        }
        public DataTable loadTable() {
            return cd.ExcuteQuery("select idPatient as 'Mã BN',fullname as 'Họ tên',gender as 'Giới tính',age as 'Tuổi',phone as 'Điện thoại',address as 'Địa chỉ',marital as 'Hôn nhân' from patient");
        }
        public DataTable loadTN(String id) {
            return cd.ExcuteQuery("select * from patient,nextofkind where idPatient='" + id + "' and patient.idNextOfKind=nextofkind.idNextOfKind");
        }
        public Boolean InsertTN(NextOfKindDAO p) {
            String sql = @"insert into nextofkind (fullname, phone, address,relationship) values(@name,@phone,@address,@relationship)";
            int count = 4;
            String[] name = new String[count];
            object[] value = new object[count];
            name[0] = "@name";
            name[1] = "@phone";
            name[2] = "@address";
            name[3] = "@relationship";
            value[0] = p.Fullname;
            value[1] = p.Phone;
            value[2] = p.Address;
            value[3] = p.Relationship;
            return cd.ExcuteNonQuery(sql, name, value, count);
        }
        public Boolean updateTN(NextOfKindDAO p)
        {
            String sql = @"update  nextofkind set fullname=@name,phone=@phone,address=@address,relationship=@relationship where idNextOfKind=@id";
            int count = 5;
            String[] name = new String[count];
            object[] value = new object[count];
            name[0] = "@name";
            name[1] = "@phone";
            name[2] = "@address";
            name[3] = "@relationship";
            name[4] = "@id";
            value[0] = p.Fullname;
            value[1] = p.Phone;
            value[2] = p.Address;
            value[3] = p.Relationship;
            value[4] = p.IdNextOfKind;
            return cd.ExcuteNonQuery(sql, name, value, count);
        }
        public Boolean deleteTN(String id)
        {
            String sql = @"delete from nextofkind where idNextOfKind = '" + id + "'";
            return cd.ExcuteNonQuery(sql);
        }
        public int getIdLast() {
            String sql = "select MAX(idNextOfKind) from nextofkind";
            dt = cd.ExcuteQuery(sql);
            return Int32.Parse(dt.Rows[0][0].ToString());
        }
        public int getIdByPatient(String id) {
            String sql = "select idNextOfKind from patient where idPatient ='"+id+"'";
            dt = cd.ExcuteQuery(sql);
            return Int32.Parse(dt.Rows[0][0].ToString());
        }
    }
}
